package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.EnrollInviteFailCause;
import com.xmy.cultivate.service.IEnrollInviteFailCauseService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 邀约失败原因 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-08-22
 */
@RestController
@RequestMapping("/api/enrollInviteFailCause")
public class EnrollInviteFailCauseController {

    @Autowired
    IEnrollInviteFailCauseService iEnrollInviteFailCauseService;

    @GetMapping("/isDispose")
    @ApiOperation("查询其它原因")
    public Result searchEnrollInviteFailCause(@RequestParam Map reMap) {
        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 10 : Integer.valueOf((String) reMap.get("pageSize"));
        Page<EnrollInviteFailCause> iPage = new Page<>(pageNum, pageSize);

        String date = (String) reMap.get("date");// 日期
        String isDispose = (String) reMap.get("isDispose");// 是否处理，0否，1是

        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.select("*,getStaffNameForId(created_id) AS teacherName");
        wrapper.eq("type", 2);
        wrapper.apply(StringUtils.isNotBlank(date), "DATE(created_at) = '" + date+"'");
        wrapper.eq(StringUtils.isNotBlank(isDispose), "is_dispose", isDispose);

        iEnrollInviteFailCauseService.page(iPage, wrapper);

        return new Result(Code.GET_OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()), Code.GET_OK_MSG);
    }

    @PutMapping("/updateIsDispose")
    @ApiOperation("处理")
    public Result updateIsDispose(@RequestBody Map reMap) {
        String id = (String) reMap.get("id");
        Integer isDispose = (Integer) reMap.get("isDispose");

        EnrollInviteFailCause enrollInviteFailCause = iEnrollInviteFailCauseService.getById(id);
        if (Objects.isNull(enrollInviteFailCause)) {
            return new Result(Code.ERROR, "", "该数据不存在");
        }
        enrollInviteFailCause.setIsDispose(isDispose);
        iEnrollInviteFailCauseService.updateById(enrollInviteFailCause);

        return new Result(Code.OK, "", Code.OK_MSG);
    }

}
